package org.keyyond.ms.business.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.keyyond.ms.business.dao.MsCodeValueDao;
import org.keyyond.ms.business.model.MsBusinessMessageLog;
import org.keyyond.ms.business.model.MsCodeValue;
import org.keyyond.ms.business.service.MsBusinessMessageLogService;
import org.keyyond.ms.business.service.MsBusinessParamService;
import org.keyyond.ms.business.util.Constant;
import org.keyyond.ms.core.util.CommonBeanUtil;
import org.keyyond.ms.core.util.ConstantResult;
import org.keyyond.ms.core.util.JsonUtil;
import org.keyyond.ms.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhuyong
 * @belong to Shccs
 * @date:2018-1-19(V1.0)
 * @date:2018-1-19(V1.0)
 * @version : V1.0
 */
@RequestMapping("sys")
@RestController
public class MsBusinessParamController {
	@Autowired
	private MsBusinessParamService paramService;

	@Autowired
	private MsBusinessMessageLogService businessMessageLogService;

	private static final String XY_PARAM_ERROR_RESULT = "{\"result\":\"false\",\"message\":\"参数有误\"}";

	private static final String COMMON_RETURN_MSG = "{\"code\":\"fail\",\"result\":\"服务异常\",\"account\":\"-1\"}";
	
	@Autowired
	private MsCodeValueDao codeValueDao;

	/**
	 * 根据code查询系统配置表的信息
	 * 
	 * @author zhuyong
	 * @since 2018-1-19
	 * @category /br>根据code查询系统配置表的信息</br>
	 * 
	 * @param request
	 *            | HttpServletRequest | 必填 请求对象
	 * @param jsonStr
	 *            | string | 必填 {code:"value"}
	 * @return <b>配置对象Json字符串</br>
	 * @exception
	 */
	@RequestMapping(value = "/queryParamByCode", method = RequestMethod.POST)
	public String querySysParamByCode(HttpServletRequest request, String param) {
		return paramService.selectParamByCode(request, param);
	}

	/**
	 * 根据分类码、ccode、公司编码、部门编码获取对应的所有参数列表，ckey，ccode必填，公司编码与部门编码可为空
	 * 
	 * @author Allan
	 * @param param={"ckey":"分组码","companycode":"公司编码","deptcode":"部门编码"}
	 * @return {"result":true,"data":{"records":[{"cdesc":"性别","seqno":"1","ccode":"1","processstatus":"","cvalue":"男","createby":"","isleaf":"Y","modifyby":"","parentid":"","companycode":"","ckey":"KEY_SEX","modifyon":null,"createon":null,"deptcode":""},{"cdesc":"性别","seqno":"2","ccode":"2","processstatus":"","cvalue":"女","createby":"","isleaf":"Y","modifyby":"","parentid":"","companycode":"","ckey":"KEY_SEX","modifyon":null,"createon":null,"deptcode":""}]}}
	 * @throws UnsupportedEncodingException
	 *             根据具体的异常类型抛出
	 */
	@RequestMapping(value = "/getCodeValueByKeyAndCompanycodeAndDeptcode", method = RequestMethod.POST)
	public String getCodeValueByKeyAndCompanycodeAndDeptcode(
			HttpServletRequest request) {
		String param = request.getParameter(Constant.CONST_ZBGS_PARAM);
		MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
		try {
			String traceId = request
					.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
			messageLog.setLogtype(Constant.CONST_LOGTYPE_SERVICE_CALL);// 日志类型
			messageLog.setContent(StringUtils.isEmpty(param) ? "" : StringUtil
					.cutString(param, 1024, "GBK"));// 截取长度,推送参数
			messageLog.setTraceid(traceId);// 微服务跟踪ID
			messageLog.setCreateby(Constant.CONST_SYSTEM);// 创建人
			messageLog.setModifyby(Constant.CONST_SYSTEM);// 修改人
			Map<String, String> paramMap = JsonUtil.JsonToMap(JSONObject
					.fromObject(param).toString());
			if (paramMap.get("ckey") == null) {
				return XY_PARAM_ERROR_RESULT;
			} else {
				MsCodeValue codeValue = new MsCodeValue();
				codeValue.setCkey((String)(paramMap.get("ckey")));
				codeValue.setCompanycode((String)(paramMap.get("companycode")));
				codeValue.setDeptcode((String)(paramMap.get("deptcode")));
				List result = codeValueDao.getCodeValueByKeyAndCompanycodeAndDeptcode(codeValue);
				//处理list，根据取数规则读取值
				JSONArray dataArray = new JSONArray();
				dataArray = JSONArray.fromObject(result);
				messageLog.setReturnmessage(dataArray.toString());
				businessMessageLogService.saveLog(messageLog);
				return CommonBeanUtil.returnResult(true, dataArray);
			}
		} catch (Exception e) {
			e.printStackTrace();
			messageLog.setReturnmessage(StringUtil.cutString(COMMON_RETURN_MSG, 1024,
					"GBK"));// 截取长度,返回消息
			businessMessageLogService.saveLog(messageLog);
			return COMMON_RETURN_MSG;
		}
	}
	
	/**
	 * 根据分类码、ccode、公司编码、部门编码获取对应的所有参数列表，ckey，ccode必填，公司编码与部门编码可为空
	* @author Allan
	* @param param={"ckey":"分组码","ccode":"参数编码","companycode":"公司编码","deptcode":"部门编码"}
	* @return {"result":true,"data":{"record":{"cdesc":"性别","seqno":"1","ccode":"1","processstatus":"","cvalue":"男","createby":"","isleaf":"Y","modifyby":"","parentid":"","companycode":"","ckey":"KEY_SEX","modifyon":null,"createon":null,"deptcode":""}}}
	* @throws UnsupportedEncodingException 根据具体的异常类型抛出
	 */
	@RequestMapping(value = "/getValueByKeyAndCodeAndCompanycodeAndDeptcode", method = RequestMethod.POST)
	public String getValueByKeyAndCodeAndCompanycodeAndDeptcode(
			HttpServletRequest request) {
		String param = request.getParameter(Constant.CONST_ZBGS_PARAM);
		MsBusinessMessageLog messageLog = new MsBusinessMessageLog();
		try {
			String traceId = request
					.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
			messageLog.setLogtype(Constant.CONST_LOGTYPE_SERVICE_CALL);// 日志类型
			messageLog.setContent(StringUtils.isEmpty(param) ? "" : StringUtil
					.cutString(param, 1024, "GBK"));// 截取长度,推送参数
			messageLog.setTraceid(traceId);// 微服务跟踪ID
			messageLog.setCreateby(Constant.CONST_SYSTEM);// 创建人
			messageLog.setModifyby(Constant.CONST_SYSTEM);// 修改人
			Map<String, String> paramMap = JsonUtil.JsonToMap(JSONObject
					.fromObject(param).toString());
			if (paramMap.get("ckey") == null || paramMap.get("ccode") == null) {
				return XY_PARAM_ERROR_RESULT;
			} else {
				MsCodeValue codeValue = new MsCodeValue();
				codeValue.setCkey((String)(paramMap.get("ckey")));
				codeValue.setCompanycode((String)(paramMap.get("companycode")));
				codeValue.setDeptcode((String)(paramMap.get("deptcode")));
				codeValue.setCcode((String)(paramMap.get("ccode")));
				MsCodeValue result = codeValueDao.getValueByKeyAndCodeAndCompanycodeAndDeptcode(codeValue);
				//处理list，根据取数规则读取值
				JSONObject message = new JSONObject();
				JSONArray dataArray = new JSONArray();
				dataArray = JSONArray.fromObject(result);
				messageLog.setReturnmessage(dataArray.toString());
				businessMessageLogService.saveLog(messageLog);
				return CommonBeanUtil.returnResult(true, dataArray);
			}
		} catch (Exception e) {
			e.printStackTrace();
			messageLog.setReturnmessage(StringUtil.cutString(COMMON_RETURN_MSG, 1024,
					"GBK"));// 截取长度,返回消息
			businessMessageLogService.saveLog(messageLog);
			return COMMON_RETURN_MSG;
		}
	}
}
